(function() { 
	appCommon.controller('userInfoCtr', userInfoCtroller);

	function userInfoCtroller($rootScope, $scope, $http, $httpParamSerializer, ngDialog) {	

		//初始化数据
		$scope.isShow = 'person';
		$rootScope.view = "";		
		$scope.saveMassage = "";
		$scope.items = [];
		$http({
			method: "POST",
			url: "/index.php?m=home&a=index&c=user"
		}).then(function successCallback(response) {
			var result = response.data.data;
			if(response.data.status) { 
				$scope.username = result[0].username;
				$scope.email = result[0].email;
				$scope.phone = result[0].phone;		
				$scope.birthday = result[0].birthday;	
				$scope.gender = result[0].gender;	
				$scope.address = result[0].address;	
				$scope.photo = result[0].photo;	
				$scope.wish = result[0].wish;	
			}else {
				//暂无404页面
				//window.location.href="/index.php"
			}
		});

		//切换到个人信息
		$scope.showPerson = function() {
			$scope.isShow = 'person';
		}

		//切换到密码
		$scope.showZM = function() {
			$scope.isShow = 'zm';
		}

		//切换到我发表的文章
		$scope.showBlog = function() {
			$scope.isShow = 'blog';
			if(!$scope.items.length) {
				$scope.getBlog()
			}
		}

		// 删除文章
		$scope.delBlog = function(post_id) {	
			$http({
				method: "POST",
				url: "/index.php?m=home&a=delBlog&c=user",
				data: $httpParamSerializer({
					post_id: post_id
				}),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).then(function successCallback(response) {
				if(response) {
					$scope.items = $scope.items.filter(function(item) {
						return item.post_id != post_id;
					})
				}
			});
		}

		// 获取文章  
		var nowPage = 1;
		var count = 4;       
		var busy = false; 
		$scope.getBlog = function() {	
			if(!busy) {
				busy = true;
				$http({
					method: "POST",
					url: "/index.php?m=home&a=getBlog&c=user",
					data: $httpParamSerializer({
						nowPage: nowPage,
						count: count
					}),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).then(function successCallback(response) {
					nowPage++;
					var items = response.data.articleList;
					for (var i = 0; i < items.length; i++) {
						$scope.items.push(items[i]);
					}

					if(items.length > 3) {                   
						busy = false;
					}else {
						$scope.noMore = "没有更多...";
						busy = true;
					}
				});
			}
		}

		//提交更新
		$scope.saveInfo = function() {	
			$http({
				method: "POST",
				url: "/index.php?m=home&a=updateInfo&c=user",
				data: $httpParamSerializer({
                    'userName': $scope.username,
                    'birthday': $scope.birthday,
					'phone' : $scope.phone,	
                    'address': $scope.address,
                    'gender': $scope.gender,
                    'wish': $scope.wish
                }),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).then(function successCallback(response) {
					var data = response.data;                    
                    $scope.saveMassage = data["message"];

                   	var dialog = ngDialog.open({      
                   		plain: true,              		
                   		template: '<div class="save-massage">'+$scope.saveMassage+'</div>', 
                   		controller: 'userInfoCtr'
                   	});      
                   	//2秒后关闭
                   	setTimeout(function(){
                        dialog.close();
                    }, 2000);     
			});
		}

		// 保存新密码
		$scope.savePassword = function() {	
			if($scope.newPassword != $scope.newPassword1) {
				$scope.newPassword1Error = true;
	            $scope.newPassword1Message = "新密码和确认密码不一致！";  
			}else {
				$scope.newPassword1Error = false;
				$http({
					method: "POST",
					url: "/index.php?m=home&a=updatePassword&c=user",
					data: $httpParamSerializer({
	                    'password': $scope.password,
	                    'newPassword': $scope.newPassword
	                }),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
					}).then(function successCallback(response) {
						var data = response.data;
	                    if(data.status) {
	                       	$scope.passwordError = false;
	                       	$scope.saveMassage = data.message;  
	                    }else{
	                        $scope.passwordError = true;
	                        $scope.saveMassage = data.message;  
	                    }

	                   	var dialog = ngDialog.open({      
	                   		plain: true,              		
	                   		template: '<div class="save-massage">'+$scope.saveMassage+'</div>', 
	                   		controller: 'userInfoCtr'
	                   	});   

	                   	//2秒后关闭
	                   	setTimeout(function(){
	                        dialog.close();
	                    }, 2000);  
				});
			}
		}

		//更新头像
		$scope.savePhoto = function(files) {
			var file = files[0];
			var datas = new FormData(document.getElementById("photoForm"));
			datas.append("CustomField", "This is some extra data");
			if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) {
				$scope.photoError = true;
				$scope.photoMessage = "图片格式不正确，请重新上传！";
			}else if(file.size > 1024 * 1024 * 4){
				$scope.photoError = true;
				$scope.photoMessage = "图片大小应小于4M！";
			}else{
				$http({
					method: "POST",
					url: "/index.php?m=home&a=updatePhoto&c=user",
					headers: {
						'Content-Type': undefined
					},
					transformRequest: function() {
						var formData = new FormData();
						formData.append('photo', document.getElementsByClassName('infoPhoto')[0].files[0]);
						return formData;
					}
					}).then(function successCallback(response) {
						var data = response.data;
	                    if(data.url) {
	                       	$scope.photo = data.url;
	                       	$scope.saveMassage = "头像修改成功！"

	                    }else{
	                        $scope.userNameError = "";
	                        $scope.saveMassage = "头像修改失败！";  
	                    }

	                    var dialog = ngDialog.open({      
	                   		plain: true,              		
	                   		template: '<div class="save-massage">'+$scope.saveMassage+'</div>', 
	                   		controller: 'userInfoCtr'
	                   	});   

	                   	//2秒后关闭
	                   	setTimeout(function(){
	                        dialog.close();
	                    }, 2000);  
				});
			}
		}	

	}
})();